<?php

function CABLE_adminFileUpload()
{
	global $cable_admin_plugin;
	
	/* if the user isn't admin, drop out immediately */
	if ($cable_admin_plugin[connector]->user->values[adminlevel] < 1)
		return true;
	
	/* switch the action template to the correct page */
	$cable_admin_plugin[action_template] = $cable_admin_plugin[action_template]->GetChild('CABLE-UploadPage');
	if ($cable_admin_plugin[action_template] === false)
		return 'Error retrieving file uploading page';
	
	/* strip any slashes from the given name */
	if ($_REQUEST['file_author'] != '')
		$author = str_replace('/','',urldecode($_REQUEST['file_author']));
	else
		$author = $cable_admin_plugin[connector]->user->values[username];
		
	/* fill out the authors selection list of the template */
	if (($ret = CABLE_adminAuthors($author)) !== true)
		return $ret;
	
	/* set the file directory now that we have the appros user */
	$file_dir = $cable_admin_plugin[connector]->config[SitePath]."users/$author/";
	
	/* go through each uploaded file and transfer it to the selected authors folder */
	if(count($_FILES) > 0)
	{
		foreach ($_FILES['file_upload'][error] as $key => $error)
		{
			$tmp_name = $_FILES['file_upload'][tmp_name][$key];
			$name = $_FILES['file_upload'][name][$key];
			$destination = $file_dir.$name;
			
			if ($name != '')
			{
				if($error == UPLOAD_ERR_OK)
				{	
					if (file_exists($destination))
					{
						$cable_admin_plugin[action_template]->SetBool('CABLE-Error',true);
						$cable_admin_plugin[action_template]->AppendInsert('CABLE-Error.text',"File '$name' already exists<br />");
					}
					else
					{
						if(move_uploaded_file($tmp_name,$destination))
						{
							$cable_admin_plugin[action_template]->SetBool('CABLE-Notice',true);
							$cable_admin_plugin[action_template]->AppendInsert('CABLE-Notice.text',"File '$name' saved successfully<br />");
						}
						else
						{
							$cable_admin_plugin[action_template]->SetBool('CABLE-Error',true);
							$cable_admin_plugin[action_template]->AppendInsert('CABLE-Error.text',"Could not transfer file '$name'<br />");
						}
					}
				}
				else
				{
					$cable_admin_plugin[action_template]->SetBool('CABLE-Error',true);
					$cable_admin_plugin[action_template]->AppendInsert('CABLE-Error.text',"Error uploading file '$name'<br />");
				}
			}
		}
	}
	
	return true;
}

function CABLE_adminFileList()
{
	global $cable_admin_plugin;

	/* if the user isn't admin, drop out immediately */
	if ($cable_admin_plugin[connector]->user->values[adminlevel] < 1)
		return true;
	
	/* switch the action template to the correct page */
	$cable_admin_plugin[action_template] = $cable_admin_plugin[action_template]->GetChild('CABLE-FilesPage');
	if ($cable_admin_plugin[action_template] === false)
		return 'Error retrieving file editing page';
		
	$file_template = $cable_admin_plugin[action_template]->GetChild('CABLE-File');
	if ($file_template === false)
		return 'Error retrieving file item template.';
	
	/* strip any slashes from the given name */
	if ($_REQUEST['file_author'] != '')
		$author = str_replace('/','',urldecode($_REQUEST['file_author']));
	else
		$author = $cable_admin_plugin[connector]->user->values[username];
	
	/* which file in the folder are we to start displaying at? */
	if (($_REQUEST['file_index'] != '') && ($_REQUEST['file_index'] > 0))
		$file_index = (int) urldecode($_REQUEST['file_index']);
	else
		$file_index = 0;
		
	/* fill out the authors selection list of the template */
	if (($ret = CABLE_adminAuthors($author)) !== true)
		return $ret;
	
	/* set the file directory now that we have the appros user */
	$file_dir = $cable_admin_plugin[connector]->config[SitePath]."users/$author/";
	
	/* apply requested function */
	switch(urldecode($_REQUEST['file_action']))
	{
		case 'delete_file':
			$file = str_replace('/','',urldecode($_REQUEST['file_name']));
			if (unlink($file_dir.$file))
			{
				$cable_admin_plugin[action_template]->SetBool('CABLE-Notice',true);
				$cable_admin_plugin[action_template]->ReplaceInsert('CABLE-Notice.text','File "'.$file.'" successfully deleted.');
			}
			else
			{
				$cable_admin_plugin[action_template]->SetBool('CABLE-Error',true);
				$cable_admin_plugin[action_template]->ReplaceInsert('CABLE-Error.text','Could not delete file "'.$file.'".');
			}
			
			break;
	}
	
	/* doublecheck for sanity, and then retrieve the files from the directory */
	$num_files = 0;
	if (is_dir($file_dir))
	{
		$file_list = array();
		if ($dir_handle = opendir($file_dir))
		{
			/* retrieve the directory contents */
			$files = scandir($file_dir);
			$num_files = count($files);
			
			/* cut out the files for the current page */
			$files = array_slice($files,$file_index,$cable_admin_plugin[connector]->config['FileListCount']);
			
			$filesize_divider = $cable_admin_plugin[action_template]->GetValue('CABLE-SizeDivider');
			$filesize_unit = $cable_admin_plugin[action_template]->GetValue('CABLE-SizeUnit');
			
			/* go through every file and retrive the necessary information */
			foreach($files as $file)
			{
				/* don't include hidden files or directories */
				if (($file != '.') && ($file != '..') && (substr($file,0,1) != '.') && (!is_dir($file_dir.$file)))
				{
					/* save the basic file information into the file data array */
					$file_list[$file] = pathinfo($file_dir.$file);
					$file_list[$file][author] = $author;
					$file_list[$file][size] = round(filesize($file_dir.$file) / $filesize_divider).$filesize_unit;
					$file_list[$file][type] = strtoupper($file_list[$file][extension]);
					
					/* if the file is an image, retrieve basic image information (width/height) */
					if (in_array(strtolower($file_list[$file][extension]),explode(',',$cable_admin_plugin[connector]->config['ImageExtensions'])))
					{
						$imagesize_info = getimagesize($file_dir.$file);
						$file_list[$file][width] = $imagesize_info[0];
						$file_list[$file][height] = $imagesize_info[1];
					}
				}
			
			}
		}
		else
			return "Error opening user \"$author\" directory.";
	}
	else
	{
		$cable_admin_plugin[action_template]->SetBool('CABLE-Error',true);
		$cable_admin_plugin[action_template]->ReplaceInsert('CABLE-Error.text','User directory does not exist for author "'.$author.'".');
		return true;
	}
	
	/* write the information stored in the file list to the template */
	foreach($file_list as $file_name => $file_data)
	{
		$file_template->Reset();
		foreach($file_data as $field_name => $field_data)
			$file_template->ReplaceInsert('CABLE-File.'.$field_name,$field_data);
		
		/* basic other replacements */
		$file_url = $file_dir = $cable_admin_plugin[connector]->config[SiteURL]."users/$author/$file_name";
		$file_template->ReplaceInsert('CABLE-File.url',$file_url);
			
		$cable_admin_plugin[action_template]->AppendInsert('CABLE-Files',$file_template->template);
	}
	
	/* do file navigation */
	if ($file_index == 0)
		$cable_admin_plugin[action_template]->SetBool('CABLE-Files.nav.next',false);
	else
	{
		$cable_admin_plugin[action_template]->SetBool('CABLE-Files.nav.next',true);
		$cable_admin_plugin[action_template]->ReplaceInsert('CABLE-Files.nav.next',$file_index - $cable_admin_plugin[connector]->config['FileListCount']);
	}

	if (($file_index + $cable_admin_plugin[connector]->config['FileListCount']) > $num_files)
		$cable_admin_plugin[action_template]->SetBool('CABLE-Files.nav.prev',false);
	else
	{
		$cable_admin_plugin[action_template]->SetBool('CABLE-Files.nav.prev',true);
		$cable_admin_plugin[action_template]->ReplaceInsert('CABLE-Files.nav.prev',$file_index + $cable_admin_plugin[connector]->config['FileListCount']);
	}
	
	/* do final replacements */
	$cable_admin_plugin[action_template]->ReplaceInsert('CABLE-Files.author',$author);
	
	return true;
}

?>